package cn.xpg.controller;

import java.util.List;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import cn.xpg.entity.Company;
import cn.xpg.service.CompanyService;
import cn.xpg.tools.PageSupport;

@Controller
public class CompanyController {

	@Resource(name = "companyService")
	private CompanyService companyService;

	// 分页查询企业信息
	@RequestMapping("/getCompanyList.do")
	public String getcompanyList(HttpServletRequest request) {
		String company_name = request.getParameter("company_name");
		String pageIndex = request.getParameter("pageIndex");
		int pageSize = 3;
		int currentPageNo = 1;
		if (pageIndex != null) {
			try {
				currentPageNo = Integer.valueOf(pageIndex);
			} catch (Exception e) {
				// TODO: handle exception
				e.printStackTrace();
			}
		}
		int totalCount = 0;
		totalCount = companyService.getCompanyCount(company_name);
		System.out.println("查询企业信息！！！！！！！！！！————>getcompanyList");

		PageSupport pages = new PageSupport();
		pages.setCurrentPageNo(currentPageNo);
		pages.setPageSize(pageSize);
		pages.setTotalCount(totalCount);

		int totalPageCount = pages.getTotalPageCount();

		if (currentPageNo < 1) {
			currentPageNo = 1;
		} else if (currentPageNo > totalPageCount) {
			currentPageNo = totalPageCount;
		}

		List<Company> companyList = null;
		companyList = companyService.getCompanyList(company_name, (currentPageNo - 1) * pageSize, pageSize);

		request.setAttribute("companyList", companyList);
		request.setAttribute("totalPageCount", totalPageCount);
		request.setAttribute("pages", pages);
		request.setAttribute("company_name", company_name);
		return "/jsp/user/manage_qy";
	}

	// 新增企业
	@RequestMapping("addCompany.do")
	public String addcompany(@Valid Company company) {
		System.out.println("新增企业信息！！！！！！！！！！————>addcompany");
		if (companyService.addCompany(company) > 0) {
			return "redirect:/getCompanyList.do";
		} else {
			return "/jsp/user/manage_qy";
		}
	}

	// 获取企业Id
	@RequestMapping("getCompanyId.do")
	public String getcompanyId(HttpServletRequest request) {
		System.out.println("获取企业Id！！！！！！！！！！————>getCompanyId");
		int company_id = Integer.parseInt(request.getParameter("company_id"));
		Company company = companyService.getCompanyId(company_id);
		request.setAttribute("company", company);
		return "/jsp/user/manage_qy_update";
	}

	// 修改企业
	@RequestMapping("updateCompany.do")
	public String updatecompany(@Valid Company company, HttpServletRequest request) {
		System.out.println("修改企业信息！！！！！！！！！！————>updateCompany");
		if (companyService.updateCompany(company) > 0) {
			System.out.println("修改成功！！！！！！");
			return "redirect:/getCompanyList.do";
		} else {
			System.out.println("修改失败！！！！！！");
			request.setAttribute("company", company);
			return "/jsp/user/manage_qy_update";
		}
	}

	// 删除企业
	@RequestMapping("deleteCompany.do")
	public String deletecompany(HttpServletRequest request) {
		System.out.println("删除企业信息！！！！！！！！！！————>deleteCompany");
		int company_id = Integer.valueOf(request.getParameter("company_id"));
		if (companyService.deleteCompany(company_id) > 0) {
			System.out.println("删除成功！！！！！！");
		} else {
			System.out.println("删除失败！！！！！！");
		}
		return "redirect:/getCompanyList.do";
	}
}

